package com.credithc.signature.manager.controller.management;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.manager.ro.ChannelRo;
import com.credithc.signature.manager.ro.ProductSignRo;
import com.credithc.signature.manager.service.ProductSignService;
import com.credithc.signature.manager.vo.ChannelVo;
import com.credithc.signature.manager.vo.ProductSignHistoryVo;
import com.credithc.signature.manager.vo.ProductSignVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghanqiu181212@credithc.com
 * @Date 2019/10/29 17:07
 */
@RestController
@RequestMapping("/manager/scs/v2/productSign/")
public class ProductSignController {

    @Autowired
    private ProductSignService productSignService;

    @Autowired
    MessageFactory messageFactory;

    /**
     * 查询所有
     *
     * @param channelRo
     * @return
     */
    @RequestMapping("/list")
    public Message list(ChannelRo channelRo) {
        PageInfo<ChannelVo> pageInfo = productSignService.queryAll(channelRo);
        Map map = new HashMap<>(16);
        map.put("pageInfo", pageInfo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 查询单条
     *
     * @param productCode
     * @return
     */
    @RequestMapping("/info")
    public Message info(String productCode) {
        ProductSignVo productSignVo = productSignService.getInfoByProductCode(productCode);
        List<ProductSignHistoryVo> historyVoList = productSignVo.getProductSignHistoryVoList();
        ChannelVo channelVo = productSignVo.getChannelVo();
        PageInfo<ProductSignHistoryVo> pageInfo = new PageInfo<>(historyVoList, historyVoList.size(), 0, 50);
        Map<String, Object> map = new HashMap<>(16);
        map.put("channelVo", channelVo);
        map.put("pageInfo", pageInfo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 新增路由配置
     *
     * @param productSignRo
     * @param token
     * @return
     */
    @RequestMapping("/save")
    public Message add(@RequestBody @Valid ProductSignRo productSignRo, @RequestHeader("token") String token) {
        String msg = productSignService.save(productSignRo, token);
        if (StringUtils.isNotBlank(msg)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, msg);
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }
}
